package com.chain.BrotherExchange.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.ExtractMoneyDetails;
import com.chain.BrotherExchange.ui.adapter.ExtractMoneyDetailsListAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.AssetsDetailsViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;

import static android.app.Activity.RESULT_OK;

/**
 * 提币明细列表
 */
public class ExtractMoneyDetailsListFragment extends AbsNetworkStateFragment
        implements OnRefreshLoadmoreListener, AdapterView.OnItemClickListener {

    @Bind(R.id.recycler_view) RecyclerView mRecyclerView;
    @Bind(R.id.smart_refresh_layout) SmartRefreshLayout mSmartRefreshLayout;

    private AssetsDetailsViewModel mViewModel;
    private ExtractMoneyDetailsListAdapter mAdapter;

    @Override
    protected int getLayoutID() {
        return R.layout.simple_load_refresh_recycler_view;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(AssetsDetailsViewModel.class);
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(this);

        mAdapter = new ExtractMoneyDetailsListAdapter(this);
        mAdapter.setCancelClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        initExtractMoneyDetails();
    }

    private void initExtractMoneyDetails() {
        mViewModel.newExtractMoneyDetails().observe(this, resource ->
                Resource.load(getContext(), resource, this, result -> {
                    mAdapter.setOrder(result);
                    mSmartRefreshLayout.finishRefresh();
                })
        );
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 取消提币
        ExtractMoneyDetails.DataBean.ListBean.ItemsBean bean = mAdapter.getItem(position);
        mViewModel.cancelExtractMoney(bean.getId()).observe(this, resource -> {
            Androids.makeText(resource.message);
            if (resource.state == Resource.Status.SUCCESS) {
                bean.setStates(3);
                mAdapter.notifyItemChanged(position);
            }
        });
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mViewModel.moreExtractMoneyDetails().observe(this, resource -> {
            switch (resource.state) {
                case EMPTY:
                    Androids.makeText(R.string.no_more);
                    break;
                case NOT_NETWORK:
                    Androids.makeText(R.string.network_error);
                    break;
                case ERROR:
                    Androids.makeText(resource.message);
                    break;
                case SUCCESS:
                    mAdapter.addOrder(resource.source);
                    break;
            }

            mSmartRefreshLayout.finishLoadmore(0);
        });
    }

    @Override
    public void onAnewRequest() {
        initExtractMoneyDetails();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initExtractMoneyDetails();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == C.REQ_CANCEL_ORDER) {
            int position = data.getIntExtra(C.EXTRA_POSITION, -1);
            mAdapter.getItem(position).setStates(3);
            mAdapter.notifyItemChanged(position);
        }
    }
}
